import { Provider } from 'mobx-react';
import { stores } from '@/stores';
import { message } from 'antd';
import { HttpRequest } from 'xo-web-base';
import { useHistory } from 'umi';
import { FileService } from './services/file';
import global from './global';
import { userStore } from './stores/user';
/**
 * 项目根组件
 * @param props
 * @returns
 */
export default function App(props: Readonly<any>) {
  const history = useHistory();

  message.config({
    maxCount: 1,
  });

  HttpRequest.setConfig({
    api: global.api,
    onErrorCode: [
      // token失效回调
      {
        code: global.authErrorCode,
        callback: () => {
          //TOKEN失效的逻辑
          message.error('请先登录');
          userStore.removeLocalStorage();
          history.push('/user/login');
        },
      },
    ],
    // 配置上传
    onUpload: async (file, onProgress) => {
      return (await FileService.upload({ multipartFile: file }, onProgress))
        .dataInfo;
    },
  });

  return <Provider {...stores}>{props.children}</Provider>;
}
